package com.dev.utils.app;


import com.dev.DevUtils;
import com.dev.utils.app.assist.ResourceAssist;
import com.dev.utils.app.assist.ResourceRaw;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

/**
 * detail: 资源文件工具类
 * @author Ttt
 */
public class ResourceUtils {
    private ResourceUtils() {
    }

    /**
     * 获取 Resources
     * @return {@link ResourceManager}
     */
    public static ResourceManager getResources() {
        return ResourceAssist.getInstance().getResources();
    }

    /**
     * 获取 int Color
     * <pre>
     *     {@link
     * </pre>
     * @param colorId R.color.id
     * @return Color
     */
    public static int getColorForInt( final int colorId) {
        return ResourceAssist.getInstance().getColor(colorId);
    }

    /**
     * 获取 Color Color
     * <pre>
     *     {@link
     * </pre>
     * @param colorId R.color.id
     * @return Color
     */
    public static Color getColorForColor( final int colorId) {
        return ResourceAssist.getInstance().getColorForColor(colorId);
    }

    public static PixelMapElement getDrawable(final int resId) {
        Resource element = null;
        try {
            //
            element = DevUtils.getContext().getResourceManager().getResource(resId);
            //Image image = new Image(DevUtils.getDevUtils());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        }
        return new PixelMapElement(element);
    }

    /**
     * 获取 String
     * @param id R.string.id
     * @return String
     */
    public static String getString( final int id) {
        try {
            return getResources().getElement(id).getString();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取 Color
     * <pre>
     * </pre>
     * @param colorId R.color.id
     * @return Color
     */
    public static int getColor(final int colorId) {
        return ResourceRaw.getInstance().getColor(colorId);
    }


    /**
     * 通过资源ID获取位图对象
     **/
    public static PixelMap getPixelMap(int resId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = DevUtils.getAbilitySlice().getResourceManager().getResource(resId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/jpeg";
            ImageSource imageSource = ImageSource.create(drawableInputStream, null);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (drawableInputStream != null) {
                    drawableInputStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static PixelMap getPixelMapFromResource(int resourceId) {
        InputStream inputStream = null;
        try {
            // 创建图像数据源ImageSource对象
            inputStream = getResources().getResource(resourceId);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream, srcOpts);

            // 设置图片参数
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException e) {
            // HiLog.info(LABEL_LOG, "IOException");
        } catch (NotExistException e) {
            //  HiLog.info(LABEL_LOG, "NotExistException");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // HiLog.info(LABEL_LOG, "inputStream IOException");
                }
            }
        }
        return null;
    }


}
